<?php

namespace App\Http\Controllers;

use App\Models\XunDianJiLu;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class DashboardController extends Controller
{

    /**
     * 月每天提交人数
     *
     * @author Eric
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function month_people(Request $request)
    {

        $month = $request->get('month', date('m'));
        $key = $request->get('key', 'uid');
        $moshi = $request->input('moshi', 'week');

        if($moshi == "week"){
            $timestr = time();
            // 得到当前是本周的第几天,默认从周日开始
            $now_day = date('w',$timestr);

            $now_day = $now_day > 0 ? ($now_day-1):$now_day;

            $start_time = date('Y-m-d', $timestr - $now_day*60*60*24).' 00:00:00';

            $end_time = date("Y-m-d 23:59:59", strtotime(" $start_time +6 day"));

        }else if($moshi == "month"){
            // 得到本月的第一天
            $start_time = date("Y-$month-01 00:00:00");
            // 得到本月的最后一天
            $end_time = date("Y-$month-d 23:59:59", strtotime(" $start_time +1 month -1 day"));
        }

        $data = [];

        // 计算时间差
        $time = ceil((strtotime($end_time)-strtotime($start_time))/60/60/24);
        // return $time;
        for ($i = 1; $i <= $time; $i++){
            $query = XunDianJiLu::query();
            $c = $i-1;
            $day_time = strtotime("$start_time + $c day");
            $day = date("d",$day_time);
            // 时间范围
            $start_time_day = date("Y-m-d 00:00:00",$day_time);
            $end_time_day = date("Y-m-d 23:59:59", $day_time);
            $num = DB::table('xun_dian_ji_lu')
                ->distinct($key)
                ->whereBetween('created_at', [$start_time_day, $end_time_day])
                ->count($key);

            $arr = [
                'year' => $day.'日',
                'value' => $num,
            ];

            $data[] = $arr;

        }

        return $this->success($data);
    }

}
